package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private DtsCategoryService dtsCategoryService;
    @Autowired
    private DtsAdminService dtsAdminService;
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="查找")
    @RequiresPermissions("admin:category:list")
    @GetMapping("/list")
    public Object categoryList(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            @RequestParam(defaultValue = "add_time") String sort,
                            @RequestParam(defaultValue = "desc") String order){

        IPage<DtsCategory> iPage = dtsCategoryService.findPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsCategory> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }

    @GetMapping("/l1")
    public Object l1(){

        List<CatVo> name = dtsCategoryService.findName();

        return ResponseUtil.ok(name);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="添加")
    @RequiresPermissions("admin:category:create")
    @PostMapping("/create")
    public Object create(@RequestBody DtsCategory dtsCategory){

        dtsCategoryService.create(dtsCategory);

        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="编辑")
    @RequiresPermissions("admin:category:update")
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory dtsCategory){

        dtsCategoryService.update(dtsCategory);

        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="删除")
    @RequiresPermissions("admin:category:delete")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory dtsCategory){

        dtsCategoryService.delete(dtsCategory);

        return ResponseUtil.ok();
    }

}
